package cn.u313.listener;

import catcode.CatCodeUtil;
import catcode.Neko;
import cn.u313.comm.conststring.Const;
import cn.u313.comm.utils.MsgUtil;
import cn.u313.comm.utils.RgexUtil;
import cn.u313.comm.utils.Util;
import cn.u313.model.MusicModel;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.message.events.PrivateMsg;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class BaseListen extends MsgUtil {

    static Map<String, MusicModel> modelMap = new HashMap<>();

    /**
     * URL编码
     * @param s
     * @return
     */
    public String e(String s){
        try {
            return URLEncoder.encode(s,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }
    }
    /**
     * 是否管理员
     * @param qq
     * @return
     */
    public static boolean isManager(String qq){
        for (String s : Const.managerQQ) {
            if(qq.equals(s)){
                return true;
            }
        }
        return false;
    }
    /**
     * 是否管理员
     * @param
     * @return
     */
    public  boolean isManager(GroupMsg groupMsg){
        String qq = getQQ(groupMsg);
        for (String s : Const.managerQQ) {
            if(qq.equals(s)){
                return true;
            }
        }
        return false;
    }
    public static boolean notManager(GroupMsg groupMsg){
        return notManager(groupMsg.getAccountInfo().getAccountCode());
    }
    public static boolean notManager(String qq){
        return !Util.get2022端午();
    }

    /**
     * 获取QQ
     * @param groupMsg
     * @return
     */
    public String getQQ(GroupMsg groupMsg){
        return groupMsg.getAccountInfo().getAccountCode();
    }
    public String getQQ(PrivateMsg groupMsg){
        return groupMsg.getAccountInfo().getAccountCode();
    }

    /**
     * 获取群号
     * @param groupMsg
     * @return
     */
    public String getGroup(GroupMsg groupMsg){
        return groupMsg.getGroupInfo().getGroupCode();
    }
    /**
     * 获取被操作的QQ
     */
    public String getQQ1(GroupMsg groupMsg){
        String msg = groupMsg.getMsg();
        Neko a = CatCodeUtil.INSTANCE.getNeko(msg);
        if(a != null){
            String q = a.get("code") ;
            if(q!=null)
                return  q;
        }
        return RgexUtil.toRgex(msg,"【(.*?)】");
    }
    /**
     * 获取被操作的QQ 可以是自己
     */
    public String getQQ2(GroupMsg groupMsg){
        String msg = groupMsg.getMsg();
        Neko a = CatCodeUtil.INSTANCE.getNeko(msg);
        if(a != null){
            String q = a.get("code") ;
            if(q!=null)
                return  q;
        }
        return getQQ(groupMsg);
    }
    /**
     * 获取被操作的QQ
     */
    public String getQQ1(PrivateMsg groupMsg){
        String msg = groupMsg.getMsg();
        Neko a = CatCodeUtil.INSTANCE.getNeko(msg);
        if(a != null){
            String q = a.get("code") ;
            if(q!=null)
                return  q;
        }
        return RgexUtil.toRgex(msg,"【(.*?)】");
    }
    /**
     * 获取被操作的QQ
     */
    public String getCode(String msg){

        return RgexUtil.toRgex(msg,"【(.*?)】");
    }
    public String getCode(GroupMsg groupMsg){

        return getCode(groupMsg.getMsg());
    }
    /**
     * 获取被操作的QQ
     */
    public int getN(GroupMsg groupMsg){
        String msg = groupMsg.getMsg();

        try {
            return new Integer(RgexUtil.toRgex(msg,"【(.*?)】"));
        } catch (Exception e) {
            return 0;
        }
    }
    /**
     * 获取被操作的QQ
     */
    public Double getDouble(GroupMsg groupMsg){
        String msg = groupMsg.getMsg();

        try {
            return new Double(RgexUtil.toRgex(msg,"【(.*?)】"));
        } catch (Exception e) {
            return 0d;
        }
    }

    /**
     * 获取消息中的at
     * @param groupMsg
     * @return
     */
    public String getAt(GroupMsg groupMsg){
        return getCAT(groupMsg,"code");
    }
    public String getCAT(GroupMsg groupMsg,String name){
        Neko a = CatCodeUtil.INSTANCE.getNeko(groupMsg.getMsg());
        if(a != null){
            String q = a.get(name) ;
            return  q;
        }
        return null;
    }


}
